package com.demodis.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.demodis.dao.ModelDaoI;
import com.demodis.domain.TblModel;
import com.demodis.util.DBUtil;

public class ModelDaoImpl extends BaseDao implements ModelDaoI {

	@Override
	public List<TblModel> getModel(boolean falg) {
		String sql = "SELECT * FROM TBL_MODEL;";
		if(!falg) {
			sql = "SELECT * FROM tbl_model WHERE modCd = -500";
		}
		List<TblModel> list = new ArrayList<TblModel>();
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				TblModel model = new TblModel();
				model.setModId(rs.getInt("modId"));
				model.setModName(rs.getString("modName"));
				model.setParMod(rs.getInt("parMod"));
				model.setModUrl(rs.getString("modUrl"));
				model.setModCd(rs.getInt("modCd"));
				model.setModDes(rs.getString("modDes"));
				list.add(model);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return list;
	}

	@Override
	public boolean save(TblModel model) {
		String sql = "INSERT INTO TBL_MODEL( MODNAME, MODDES, PARMOD, MODCD, MODURL) VALUES(?, ?, ?, ?, ?);";
		conn = DBUtil.getConnection();
		int i = 0;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, model.getModName());
			ps.setString(2, model.getModDes());
			ps.setInt(3, model.getParMod() == null ? -1 : model.getParMod());
			ps.setInt(4, model.getModCd());
			ps.setString(5, model.getModUrl());

			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1 ? true : false;
	}

	@Override
	public boolean delete(TblModel model) {
		String sql1 = "SELECT modId FROM TBL_MODEL where parMod = ?;";
		try {
			conn = DBUtil.getConnection();
			String sql2 = "delete from tbl_model where modid = ?";
			
			ps = conn.prepareStatement(sql2);
			ps.setInt(1, model.getId());
			ps.executeUpdate();
			ps = conn.prepareStatement(sql1);
			ps.setInt(1, model.getId());
			rs = ps.executeQuery();
			StringBuilder ids = new StringBuilder();
			ids.append(model.getId());
			while (rs.next()) {
				model.setId(rs.getInt(1));
				delete(model);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return true;
	}

	@Override
	public boolean modify(TblModel model) {
		String sql = "UPDATE TBL_MODEL SET MODNAME = ?, MODDES = ?, PARMOD = ?, MODCD = ?,MODURL = ? WHERE MODID = ?;";
		int i = 0;
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(sql);
			
			ps.setString(1, model.getText());
			ps.setString(2, model.getModDes());
			ps.setInt(3, model.getParMod() == null ? -1 : model.getParMod());
			ps.setInt(4, model.getModCD());
			ps.setString(5, model.getModUrl());
			ps.setInt(6, model.getId());

			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1 ? true : false;
	}
}
